package com.ymt.bpm.web.rest.engine.process;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.ymt.bpm.model.PTaskTodo;
import com.ymt.bpm.service.engine.task.TiService;
import com.ymt.bpm.service.platform.log.SysLogService;
import com.ymt.bpm.util.AES;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/19.
 */
@RestController
@RequestMapping("/api/v1/engine/process/ti")
public class TiResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TiService tis;

    @Autowired
    private SysLogService sls;

    @GetMapping("/saveDraft/{tiId}")
    public JSONObject saveDraft(HttpServletRequest req,
                                @PathVariable("tiId")String tiId,
                                @RequestParam(value="title")String title,
                                @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("saveDraft tenantId="+tenantId);
            log.debug("saveDraft loginName="+loginName);
            log.debug("saveDraft tiId="+tiId);
            log.debug("saveDraft title="+title);
        }
        try {
            title = URLDecoder.decode(new String(Base64Utils.decodeFromString(title), Const.ENCODING), Const.ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (log.isDebugEnabled()) {
            log.debug("saveDraft title="+title);
        }
        return tis.saveDraft(tenantId, loginName, tiId, title, lang, timezoneOffset);
    }

    @PostMapping("/complete/{tiId}")
    public JSONObject complete(HttpServletRequest req,
                                @PathVariable("tiId")String tiIdEnc,
                                @RequestParam("params")String params) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("complete tenantId="+tenantId);
            log.debug("complete loginName="+loginName);
            log.debug("complete tiId="+tiId);
            log.debug("complete params="+params);
        }
        return tis.complete(tenantId, loginName, tiId, JSONObject.parseObject(params));
    }

    /*@GetMapping("/getRejectingActivities/{tiId}")
    public JSONArray getRejectingActivities(HttpServletRequest req,
                                            @PathVariable("tiId")String tiId,
                                            @RequestParam(value="activityId")String activityId,
                                            @RequestParam(value="piEid")String piEid) {
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("getRejectingActivities tenantId="+tenantId);
            log.debug("getRejectingActivities tiId="+tiId);
            log.debug("getRejectingActivities activityId="+activityId);
            log.debug("getRejectingActivities piEid="+piEid);
        }
        return tis.getRejectingActivities(tenantId, tiId, activityId, piEid);
    }*/

    @PostMapping("/return/{tiId}")
    public JSONObject returnTask(HttpServletRequest req,
                                 @PathVariable("tiId")String tiIdEnc,
                                 @RequestParam("params")String params) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("returnTask tenantId="+tenantId);
            log.debug("returnTask loginName="+loginName);
            log.debug("returnTask tiId="+tiId);
            log.debug("returnTask params="+params);
        }
        return tis.returnTask(tenantId, loginName, displayName, tiId,
                JSONObject.parseObject(params));
    }

    @PostMapping("/reassign/{tiId}")
    public JSONObject reassignTask(HttpServletRequest req,
                                 @PathVariable("tiId")String tiIdEnc,
                                 @RequestParam("assignTo")String assignTo,
                                 @RequestParam("assignToName")String assignToName,
                                 @RequestParam("comment")String comment) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("reassignTask tenantId="+tenantId);
            log.debug("reassignTask loginName="+loginName);
            log.debug("reassignTask displayName="+displayName);
            log.debug("reassignTask tiId="+tiId);
            log.debug("reassignTask assignTo="+assignTo);
            log.debug("reassignTask assignToName="+assignToName);
            log.debug("reassignTask comment="+comment);
        }
        return tis.reassignTask(tenantId, loginName, displayName, tiId, assignTo, assignToName, comment);
    }

    @PostMapping("/involve/{tiId}")
    public JSONObject involve(HttpServletRequest req,
                                   @PathVariable("tiId")String tiIdEnc,
                                   @RequestParam("comment")String comment,
                                   @RequestParam("involveType")String involveType,
                                   @RequestParam("involvedUsers")String involvedUsers,
                                   @RequestParam("params")String paramsStr) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("involve tenantId="+tenantId);
            log.debug("involve loginName="+loginName);
            log.debug("involve displayName="+displayName);
            log.debug("involve tiId="+tiId);
            log.debug("involve comment="+comment);
            log.debug("involve involveType="+involveType);
            log.debug("involve involvedUsers="+involvedUsers);
            log.debug("involve paramsStr="+paramsStr);
        }
        return tis.involve(tenantId, loginName, displayName, tiId, comment,
                involveType, involvedUsers, JSONObject.parseObject(paramsStr));
    }

    @GetMapping("/defer/{tiId}")
    public JSONObject defer(HttpServletRequest req,
                              @PathVariable("tiId")String tiIdEnc) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("defer tenantId="+tenantId);
            log.debug("defer loginName="+loginName);
            log.debug("defer tiId="+tiId);
        }
        return tis.defer(tenantId, loginName, tiId);
    }

    @GetMapping("/claim/{tiId}")
    public JSONObject claim(HttpServletRequest req,
                            @PathVariable("tiId")String tiIdEnc,
                            @RequestParam("piEid")String piEid,
                            @RequestParam("activityId")String activityId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("claim tenantId="+tenantId);
            log.debug("claim loginName="+loginName);
            log.debug("claim tiId="+tiId);
            log.debug("claim piEid="+piEid);
            log.debug("claim activityId="+activityId);
        }
        return tis.claim(tenantId, loginName, tiId, piEid, activityId);
    }

    @GetMapping("/unclaim/{tiId}")
    public JSONObject unclaim(HttpServletRequest req,
                            @PathVariable("tiId")String tiIdEnc,
                            @RequestParam("piEid")String piEid,
                            @RequestParam("activityId")String activityId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("unclaim tenantId="+tenantId);
            log.debug("unclaim loginName="+loginName);
            log.debug("unclaim tiId="+tiId);
            log.debug("unclaim piEid="+piEid);
            log.debug("unclaim activityId="+activityId);
        }
        return tis.unclaim(tenantId, loginName, tiId, piEid, activityId);
    }

}
